package com.example.service.impl;

import com.example.mapper.TbBookBaseMapper;
import com.example.mapper.TbBookMapper;
import com.example.pojo.TbBookBase;
import com.example.service.TbBookService;
import com.example.pojo.TbBook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author:20201002983_郑雨蓓
 * Date: 2022/12/15
 * Description:
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = -1, readOnly = false, rollbackFor = {Exception.class})
public class TbBookServiceImpl implements TbBookService {
    @Resource
    TbBookMapper tbBookMapper;
    @Resource
    TbBookBaseMapper tbBookbaseMapper;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, isolation = Isolation.DEFAULT, timeout = -1, readOnly = true, rollbackFor = {Exception.class})
    public List<TbBook> list() {
        return tbBookMapper.list();
    }

    @Override
    public TbBook selectById(Long bookId) {
        return tbBookMapper.selectById(bookId);
    }

    @Override
    public List<TbBook> selectByCategoryId(Long cid) {
        return tbBookMapper.selectByCategoryId(cid);
    }

    @Override
    public List<TbBook> selectByName(String bookName) {
        return tbBookMapper.selectByName(bookName);
    }

    @Override
    public List<TbBook> selectByMulCon(TbBook tbBook) {
        return tbBookMapper.selectByMulCon(tbBook);
    }

    @Override
    public int add(TbBook tbBook) {
        return tbBookMapper.add(tbBook);
    }

    @Override
    public int update(TbBook tbBook) {
        int num = tbBookMapper.update(tbBook);
        //相应的也修改详情页的价格
        if (tbBook.getSellingPrice() != null)
            num += tbBookbaseMapper.updateSellingPrice(tbBook.getSellingPrice(), tbBook.getBookId());
        return num;
    }

    @Override
    public int delete(Long id) {

        int temp = tbBookbaseMapper.delete(id);
        temp += tbBookMapper.delete(id);
        return temp;
    }


}
